function hfile = fmr_GenerateSTC(hfile, stcprefix)
% FMR::GenerateSTC  - create empty STC files
%
% FORMAT:       [fmr] = fmr.GenerateSTC([stcprefix])
%
% Input fields:
%
%       stcprefix   if given, use this as new STC prefix
%
% Output fields:
%
%       fmr         altered FMR object
%
% Note: the FMR must be saved prior to calling this method!

% Version:  v0.7b
% Build:    7083009
% Date:     Aug-30 2007, 9:35 AM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'fmr')
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
sc = bvqxfile_getscont(hfile.L);
bc = sc.C;
if nargin < 2 || ...
   ~ischar(stcprefix) || ...
    isempty(stcprefix) || ...
    numel(stcprefix) > 128 || ...
    any(stcprefix(:)' == '.' | stcprefix(:)' == '/' | stcprefix(:)' == '\')
    stcprefix = bc.Prefix(:)';
else
    stcprefix = stcprefix(:)';
end

% is FMR saved?
fmrfile = sc.F;
if isempty(fmrfile) || ...
    exist(fmrfile(:)', 'file') ~= 2
    error( ...
        'BVQXfile:BadArgument', ...
        'The FMR object must be saved first.' ...
    );
end
fmrpath = fileparts(fmrfile(:)');

% unload current slices
bc.Slice = [];
bvqxfile_setcont(hfile.L, bc);

% get some settings
dt = bc.NrOfVolumes;
dx = bc.ResolutionX;
dy = bc.ResolutionY;
dz = bc.NrOfSlices;

% depending on FileVersion -> < 4 multiple files
fv = bc.FileVersion;
if fv < 5 || ...
    bc.DataStorageFormat == 1
    
    % loop over slices
    for sc = 1:dz
        
        % generate STC file
        stcfile = sprintf('%s/%s%d.stc', fmrpath, stcprefix, sc);
        try
            stci = fopen(stcfile, 'w', 'ieee-le');
            if stci < 1
                error('FILE_OPEN_ERROR');
            end
            fwrite(stci, [dx, dy], 'uint16');
            fwrite(stci, zeros(dx * dy * dt, 1), 'uint16');
            fclose(stci);
            bc.Slice(sc).STCData = ...
                transio(stcfile, 'ieee-le', 'uint16', 4, [dx, dy, dt]);
        catch
            aft_ClearObject(sth);
            error( ...
                'BVQXfile:ErrorSavingFile', ...
                'Error saving STC ''%s''.', ...
                stcfile ...
            );
        end
    end

% otherwise one file only
else
    
    % generate STC filename
    stcfile = sprintf('%s/%s.stc', fmrpath, stcprefix);
    
    % open and write content
    stcfid = fopen(stcfile, 'wb');
    if stcfid < 1
        error( ...
            'BVQXfile:ErrorSavingFile', ...
            'Error saving STC ''%s''.', ...
            stcfile ...
        );
    end
    
    % write content
    ne = dx * dy * dz * dt;
    fz = uint16(zeros(1048576,1));
    while ne > 1048576
        fwrite(stcfid, fz, 'uint16');
        ne = ne - 1048576;
    end
    if ne > 0
        fwrite(stcfid, fz(1:ne), 'uint16');
    end
    
    % close file
    fclose(stcfid);
    
    % check file size
    stcdir = dir(stcfile);
    if isempty(stcdir) || ...
        stcdir(1).bytes ~= (2 * dx * dy * dz * dt)
        error( ...
            'BVQXfile:ErrorSavingFile', ...
            'Error saving STC ''%s''.', ...
            stcfile ...
        );
    end
    
    % set in array
    switch (bc.DataStorageFormat)
        case {2}
            tio = transio(stcfile, 'ieee-le', 'uint16', 0, [dx, dy, dt, dz]);
        case {3}
            tio = transio(stcfile, 'ieee-le', 'uint16', 0, [dx, dy, dz, dt]);
        case {4}
            tio = transio(stcfile, 'ieee-le', 'uint16', 0, [dt, dx, dy, dz]);
        otherwise
            error( ...
                'BVQXfile:FieldUnsupported', ...
                'Unsupported DataStorageFormat.' ...
            );
    end
    bc.Slice.STCData = tio;
end

% set new prefix
bc.Prefix = stcprefix;

% set back
bvqxfile_setcont(hfile.L, bc);
